﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using System.Data;
using CrystalDecisions.Shared;
using System.IO;
using System.Text;
using System.Web.UI.HtmlControls;

namespace UI.Tesoreria
{
    public partial class FrmReporteAlumnosMatriculados : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CargarEspecialidades();

               
            }
        }

        private void CargarEspecialidades() {
            LabelError.Text = "";
            try
            {
                ReporteNominaComplementaria_BL rnc = new ReporteNominaComplementaria_BL();
                comboEspecialidad.DataSource = rnc.ObtenerEspecialidades();
                comboEspecialidad.DataTextField = "DESCRIPCCION";
                comboEspecialidad.DataValueField = "ID";
                comboEspecialidad.DataBind();
                comboEspecialidad.Items.Insert(0, "--Todos--");
            }
            catch (Exception ex)
            {

                LabelError.Text=ex.Message;
            }
        
        }

        private bool GenerarConsultaReporteComprobanteVenta()
        {
            
            try
            {
                Reporte_AlumnosQuePagaronMatricula reporte = new Reporte_AlumnosQuePagaronMatricula();
                ReporteComprobanteVenta_BL rcv = new ReporteComprobanteVenta_BL();

                DataTable data = ObtenerDataSource();

                if (data.Rows.Count == 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('" + "No se encontraron registros :" + "" + "');", true);
                    return false;
                }

                reporte.SetDataSource(data);

                //reporte.DataDefinition.FormulaFields["Docente"].Text = "'" + nombreProfeReporte + "'";

                // reporte.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, "CpVenta");

                ExportOptions CrExportOptions;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                //String rutaarchivo = System.IO.Path.GetTempFileName() + "CpVenta-" + Guid.NewGuid() + ".pdf";
                String rutaarchivo = System.IO.Path.GetTempFileName() + Guid.NewGuid() + ".pdf";
                CrDiskFileDestinationOptions.DiskFileName = rutaarchivo;

                Session["RutaReporte"] = rutaarchivo;
                Session["Reporte"] = reporte;


                CrExportOptions = reporte.ExportOptions;
                {
                    CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                    CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                    CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;

                    CrExportOptions.FormatOptions = CrFormatTypeOptions;
                }

                reporte.Export();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }



        private bool GenerarConsultaReporteComprobanteVenta2()
        {

            try
            {
                Reporte_AlumnosQuePagaronMatricula2 reporte = new Reporte_AlumnosQuePagaronMatricula2();
                ReporteComprobanteVenta_BL rcv = new ReporteComprobanteVenta_BL();

                DataTable data = ObtenerDataSource2();

                if (data.Rows.Count == 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('" + "No se encontraron registros :" + "" + "');", true);
                    return false;
                }

                reporte.SetDataSource(data);

                //reporte.DataDefinition.FormulaFields["Docente"].Text = "'" + nombreProfeReporte + "'";

                // reporte.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, "CpVenta");

                ExportOptions CrExportOptions;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                //String rutaarchivo = System.IO.Path.GetTempFileName() + "CpVenta-" + Guid.NewGuid() + ".pdf";
                String rutaarchivo = System.IO.Path.GetTempFileName() + Guid.NewGuid() + ".pdf";
                CrDiskFileDestinationOptions.DiskFileName = rutaarchivo;

                Session["RutaReporte"] = rutaarchivo;
                Session["Reporte"] = reporte;


                CrExportOptions = reporte.ExportOptions;
                {
                    CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                    CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                    CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;

                    CrExportOptions.FormatOptions = CrFormatTypeOptions;
                }

                reporte.Export();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        private DataTable ObtenerDataSource2()
        {
            try
            {
                ReporteComprobanteVenta_BL rcv = new ReporteComprobanteVenta_BL();

                String especialidad = comboEspecialidad.SelectedItem.Value;
                String alumno = txtNombreAlumno.Text;

                DataTable data = rcv.ReporteAlumnosQuePagaronMatricula2(especialidad, alumno);
                return data;
            }
            catch (Exception)
            {

                throw;
            }

        }

        private DataTable ObtenerDataSource() {
            try
            {
                ReporteComprobanteVenta_BL rcv = new ReporteComprobanteVenta_BL();

                String especialidad = comboEspecialidad.SelectedItem.Value;
                String alumno = txtNombreAlumno.Text;

                DataTable data = rcv.ReporteAlumnosQuePagaronMatricula(especialidad, alumno);
                return data;
            }
            catch (Exception)
            {
                
                throw;
            }
        
        }

        protected void btnReporte_Click(object sender, EventArgs e)
        {
            LabelError.Text = "";
            try
            {
                if (GenerarConsultaReporteComprobanteVenta())
                {

                    string script = "AbrirVentana('frmReporteComprobanteVentaPdf.aspx')";
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "popup", script, true);
                }
            }
            catch (Exception ex)
            {
                LabelError.Text = ex.Message;
               
            }
        }


        protected void btnExportar_Click(object sender, ImageClickEventArgs e)
        {
            DataTable tabla = ObtenerDataSource();

            if (tabla.Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                Page pagina = new Page();
                HtmlForm form = new HtmlForm();
                GridView dg = new GridView();
                dg.EnableViewState = false;
                dg.DataSource = tabla;
                dg.DataBind();
                pagina.EnableEventValidation = false;
                pagina.DesignerInitialize();
                pagina.Controls.Add(form);
                form.Controls.Add(dg);
                pagina.RenderControl(htw);
                Response.Clear();
                Response.Buffer = true;
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", "attachment;filename=REPORTEALUMNOSMATRICULADOS.xls");
                Response.Charset = "UTF-8";
                Response.ContentEncoding = Encoding.Default;
                Response.Write(sb.ToString());
                Response.End();
            }
        }

        protected void btnReporte0_Click(object sender, EventArgs e)
        {
            LabelError.Text = "";
            try
            {
                if (GenerarConsultaReporteComprobanteVenta2())
                {

                    string script = "AbrirVentana('frmReporteComprobanteVentaPdf.aspx')";
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "popup", script, true);
                }
            }
            catch (Exception ex)
            {
                LabelError.Text = ex.Message;

            }
        }

        protected void btnExportar0_Click(object sender, ImageClickEventArgs e)
        {
            DataTable tabla = ObtenerDataSource2();

            if (tabla.Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                Page pagina = new Page();
                HtmlForm form = new HtmlForm();
                GridView dg = new GridView();
                dg.EnableViewState = false;
                dg.DataSource = tabla;
                dg.DataBind();
                pagina.EnableEventValidation = false;
                pagina.DesignerInitialize();
                pagina.Controls.Add(form);
                form.Controls.Add(dg);
                pagina.RenderControl(htw);
                Response.Clear();
                Response.Buffer = true;
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", "attachment;filename=REPORTEALUMNOSMATRICULADOS2.xls");
                Response.Charset = "UTF-8";
                Response.ContentEncoding = Encoding.Default;
                Response.Write(sb.ToString());
                Response.End();
            }
        }

    }


    
}